

#include "delay.h"
#include "printf.h"
#include <stdbool.h>
#include <stdio.h>
#include <stm32f10x.h>

void TIM2_init() {
    NVIC_InitTypeDef NVIC_initial;
    TIM_TimeBaseInitTypeDef TIM_initial;

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

    // config
    TIM_initial.TIM_Period = 10000 - 1;
    TIM_initial.TIM_Prescaler = 7200 - 1;
    TIM_initial.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM2, &TIM_initial);

    NVIC_SetPriorityGrouping(NVIC_PriorityGroup_2);

    NVIC_initial.NVIC_IRQChannelSubPriority = 2;
    NVIC_initial.NVIC_IRQChannelSubPriority = 2;
    NVIC_initial.NVIC_IRQChannelCmd = ENABLE;
    NVIC_initial.NVIC_IRQChannel = TIM2_IRQn;
	NVIC_Init(&NVIC_initial);
	
    TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
    TIM_Cmd(TIM2, ENABLE);
}

void TIM2_IRQHandler() {
    if (TIM_GetITStatus(TIM2, TIM_IT_Update) == SET) {
        printf("Timer 2 runing\r\n");
        TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
    }
}

int main() {

    printf_init(9600);
    delay_init();
	TIM2_init();

    while (true) {

        // printf("__initial_sp = %#X\r\n", __initial_sp);
        // printf("__heap_base = %#X\r\n", __heap_base);
        // printf("__heap_limit = %#X\r\n", __heap_limit);
		printf("main running\r\n");
        delay_s(3);
    }

    return 0;
}
